﻿using System;
using System.Windows.Forms;
using System.Collections;
using System.Text;

namespace Walkmen
{
    public class SongItemComparer: IComparer
    {
        #region IComparer メンバ

        public int Compare(object x, object y)
        {
            return Compare((ListViewItem)x, (ListViewItem)y);
        }

        #endregion

        public int Compare(ListViewItem x, ListViewItem y)
        {
            int result = CompareAlbum(x, y);
            if (result == 0)
            {
                result = CompareTrack(x, y);
                if (result == 0)
                {
                    result = CompareFileName(x, y);
                }
            }

            return result;
        }

        private int CompareAlbum(ListViewItem x, ListViewItem y)
        {
            return String.Compare(x.Text, y.Text, StringComparison.Ordinal);
        }

        private int CompareTrack(ListViewItem x, ListViewItem y)
        {
            int xTrack;
            Int32.TryParse(x.SubItems[1].Text, out xTrack);

            int yTrack;
            Int32.TryParse(y.SubItems[1].Text, out yTrack);

            return (xTrack - yTrack);
        }

        private int CompareFileName(ListViewItem x, ListViewItem y)
        {
            return String.Compare((string)x.Tag, (string)y.Tag, StringComparison.Ordinal);
        }
    }
}
